package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Category;
import com.java.second_goods.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类信息
 */
@RestController
@RequestMapping("/api/category")
@Tag(name = "商品分类管理", description = "商品分类相关接口")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取商品分类列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取商品分类列表", description = "获取商品分类列表")
    public AjaxResult list(Category category) {
        List<Category> list = categoryService.selectCategoryList(category);
        return success(list);
    }

    /**
     * 获取商品分类详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取商品分类详细信息", description = "根据分类ID获取商品分类详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(categoryService.selectCategoryById(id));
    }

    /**
     * 新增商品分类
     */
    @PostMapping
    @Operation(summary = "新增商品分类", description = "新增商品分类")
    public AjaxResult add(@RequestBody Category category) {
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改商品分类
     */
    @PutMapping
    @Operation(summary = "修改商品分类", description = "修改商品分类")
    public AjaxResult edit(@RequestBody Category category) {
        return toAjax(categoryService.updateCategory(category));
    }

    /**
     * 删除商品分类
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除商品分类", description = "删除商品分类")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(categoryService.deleteCategoryByIds(ids));
    }

    /**
     * 获取所有启用的分类
     */
    @GetMapping("/enabled")
    @Operation(summary = "获取所有启用的分类", description = "获取所有启用的分类")
    public AjaxResult getEnabledCategories() {
        List<Category> categories = categoryService.selectEnabledCategories();
        return success(categories);
    }

    /**
     * 更新分类状态
     */
    @PutMapping("/status/{categoryId}/{status}")
    @Operation(summary = "更新分类状态", description = "更新分类状态")
    public AjaxResult updateStatus(@PathVariable("categoryId") Integer categoryId,
                                   @PathVariable("status") Integer status) {
        return toAjax(categoryService.updateCategoryStatus(categoryId, status));
    }
}